"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : 信号量不足启动数据库
Create At   : 2020/10/10
Owner       : @peilinqian
Description :
    1、修改max_connections参数为800000
    2、重启数据库
    3、恢复max_connections
    4、重启数据库
Expect      :
    1、修改max_connections失败
    2、重启数据库c成功
    3、恢复max_connections成功
    4、重启数据库成功
History     :
    modified: 2020/12/26  modified by @peilinqian,issueI2A5OC解决
"""
import unittest
from yat.test import Node
from yat.test import macro
import sys

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *

logger = Logger()
common = Common()
commonshpri = CommonSH('PrimaryDbUser')

class ModifyConnection(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')    
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    Constant = Constant()

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info("-----------Opengauss_Reliability_OS_Case004 start-----------")

    def test_modify_pidfile(self):
        logger.info("-----------modify max_connections-----------")
        result = commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, "max_connections = 800000")
        self.assertFalse(result)
        
        logger.info("-----------restart opengauss-----------")
        result = commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = commonshpri.startDbCluster()
        self.assertTrue(result)
        
        logger.info("-----------modify max_connections-----------")
        result = commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, "max_connections = 5000")
        self.assertTrue(result)
        
        logger.info("-----------restart opengauss-----------")
        result = commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = commonshpri.startDbCluster()
        self.assertTrue(result)
        


    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info("-----------modify max_connections-----------")
        result = commonshpri.executeGsguc('set', self.Constant.GSGUC_SUCCESS_MSG, "max_connections = 5000")
        self.assertTrue(result)
        
        logger.info("-----------restart opengauss-----------")
        result = commonshpri.stopDbCluster()
        self.assertTrue(result)
        result = commonshpri.startDbCluster()
        self.assertTrue(result)
